var webpack = require('webpack');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var HtmlWebpackPlugin = require('html-webpack-plugin');

var WEBPACK_ENV = process.env.WEBPACK_ENV || 'dev'; //环境变量配置
//htmlWebpackPlugin 的配置
var getHtmlConfig = function(name, title) {
    return {
        template: './src/view/' + name + '.html',
        filename: './view/' + name + '.html',
        title: title,
        inject: 'true',
        hash: 'true',
        chunks: ['common', name],
        favicon:"./favicon.ico"
    }

}

var config = {
    entry: {
        'common': ['./src/page/common/index.js'],
        'index': ['./src/page/index/index.js'],
        'list': ['./src/page/list/index.js'],
        'detail':['./src/page/detail/index.js'],
        'cart':['./src/page/cart/index.js'],
        'order-confirm':['./src/page/order-confirm/index.js'],
        'order-list':['./src/page/order-list/index.js'],
        'order-detail':['./src/page/order-detail/index.js'],
        'payment':['./src/page/payment/index.js'],
        'user-login': ['./src/page/user-login/index.js'],
        'result': ['./src/page/result/index.js'],
        'user-register': ['./src/page/user-register/index.js'],
        'user-pass-reset': ['./src/page/user-pass-reset/index.js'],
        'user-center': ['./src/page/user-center/index.js'],
        'user-center-update': ['./src/page/user-center-update/index.js'],
        'about': ['./src/page/about/index.js'],
    },
    output: {
         path: __dirname+'/dist/',
        publicPath: 'dev'===WEBPACK_ENV ? '/dist/':'//s.tomystery.xin/mall-fe/dist/', //判断是不是线上的打包环境
        filename: 'js/[name].js'
    },
    externals: {
        '$': 'window.jQuery'
    },
    module: {
        // noParse: [],
        loaders: [
            { test: /\.css$/, loader: ExtractTextPlugin.extract("style-loader", "css-loader") },
            { test: /\.(gif|jpg|png|woff|svg|eot|ttf|jpeg)\??.*$/, loader: 'url-loader?limit=1000&name=resource/[name].[ext]' },
            { test: /\.string$/, loader: 'html-loader' }
        ]
    },
    resolve: {
        alias: {
            node_modules: __dirname + '/node_modules',
            util: __dirname + '/src/util',
            page: __dirname + '/src/page',
            service: __dirname + '/src/service',
            image: __dirname + '/src/image'
        }
    },
    plugins: [
        // 提出公共部分 
        new webpack.optimize.CommonsChunkPlugin({
            name: 'common',
            filename: 'js/base.js'
        }),
        // 单独处理css
        new ExtractTextPlugin('css/[name].css'),

        new HtmlWebpackPlugin(getHtmlConfig('index', '首页')),
        new HtmlWebpackPlugin(getHtmlConfig('list', '商品列表')),
        new HtmlWebpackPlugin(getHtmlConfig('detail', '商品详情列表')),
        new HtmlWebpackPlugin(getHtmlConfig('user-login', '用户登录')),
        new HtmlWebpackPlugin(getHtmlConfig('cart', '购物车')),
        new HtmlWebpackPlugin(getHtmlConfig('order-confirm', '订单确认')),
        new HtmlWebpackPlugin(getHtmlConfig('order-list', '订单列表')),
        new HtmlWebpackPlugin(getHtmlConfig('order-detail', '订单信息')),
        new HtmlWebpackPlugin(getHtmlConfig('payment', '支付页面')),
        new HtmlWebpackPlugin(getHtmlConfig('result', '操作结果')),
        new HtmlWebpackPlugin(getHtmlConfig('user-register', '注册')),
        new HtmlWebpackPlugin(getHtmlConfig('user-pass-reset', '密码重置')),
        new HtmlWebpackPlugin(getHtmlConfig('user-center', '个人中心')),
        new HtmlWebpackPlugin(getHtmlConfig('user-center-update', '个人中心更新')),
        new HtmlWebpackPlugin(getHtmlConfig('user-pass-update', '更新密码')),
        new HtmlWebpackPlugin(getHtmlConfig('about', '关于我们'))
       

    ],
    devServer: { //这是线上部署必须要删除的
        port:8010,
        proxy: {
            '**/*.do': {
                target: 'http://test.happymmall.com',
                changeOrigin:true
            }
        }
    }
}


module.exports = config;
